﻿using eService;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace eService.BLL
{
    public class ExcuteCAUHINH
    {
        eServiceEntities dbEntities;
        IExecuteData<CONFIGDATEINFO> ExecuteDate;

        //IDbConnection dbConnection;
        private string[] numberArr = new string[10];
        private string[] moneyArr = new string[4];
        public string ErrMsg { get; set; }
        public bool KQ { get; set; }

        public ExcuteCAUHINH()
        {
            dbEntities = ConnectionProvider.getDbEntities();
            ExecuteDate = new ExecuteData<CONFIGDATEINFO>(dbEntities);
        }

        public CONFIGDATEINFO GetObjDate()
        {
            List<CONFIGDATEINFO> list = (List<CONFIGDATEINFO>)ExecuteDate.GetAll();
            if (list != null && list.Count > 0) KQ = true;
            CONFIGDATEINFO u = list[0];
            if (u != null) KQ = true;
            return u;
        }
        public bool UpdateObjDate(short Datetype, string DatePoint)
        {
            KQ = false;
            CONFIGDATEINFO obj = dbEntities.CONFIGDATEINFOes.First();
            CONFIGDATEINFO obj1 = new CONFIGDATEINFO();
            obj1.TypeDate = Datetype;
            obj1.PointDate = DatePoint;
            KQ = ExecuteDate.Update(obj1, obj);
            return KQ;
        }
        public string GetStartTime()
        {
            string time = "";
            CONFIGDATEINFO u = GetObjDate();
            if (u != null)
            {
                time = u.PointDate;
                time = time.Substring(0, time.IndexOf(',')).Replace(",", "").Replace("\"", "");
            }
            return time;
        }
        public string GetEndTime()
        {
            string time = "";
            CONFIGDATEINFO u = GetObjDate();
            if (u != null)
            {
                time = u.PointDate;
                time = time.Substring(time.LastIndexOf(',')).Replace(",", "").Replace("\"", "");
            }
            return time;
        }

        public string DateFormating(string InputStr)
        {
            string result;
            try
            {
                DateTime.Parse("12/15/2009");
                string text;
                if (InputStr.Substring(1, 1) == "/" && InputStr.Substring(3, 1) == "/")
                {
                    text = string.Concat(new string[]
					{
						"0",
						InputStr.Substring(2, 1),
						"/0",
						InputStr.Substring(0, 1),
						"/",
						InputStr.Substring(4, 4)
					});
                }
                else
                {
                    if (InputStr.Substring(2, 1) == "/" && InputStr.Substring(4, 1) == "/")
                    {
                        text = string.Concat(new string[]
						{
							"0",
							InputStr.Substring(3, 1),
							"/",
							InputStr.Substring(0, 2),
							"/",
							InputStr.Substring(5, 4)
						});
                    }
                    else
                    {
                        if (InputStr.Substring(1, 1) == "/" && InputStr.Substring(4, 1) == "/")
                        {
                            text = string.Concat(new string[]
							{
								InputStr.Substring(2, 2),
								"/0",
								InputStr.Substring(0, 1),
								"/",
								InputStr.Substring(5, 4)
							});
                        }
                        else
                        {
                            if (InputStr.Substring(2, 1) == "/" && InputStr.Substring(5, 1) == "/")
                            {
                                text = string.Concat(new string[]
								{
									InputStr.Substring(3, 2),
									"/",
									InputStr.Substring(0, 2),
									"/",
									InputStr.Substring(6, 4)
								});
                            }
                            else
                            {
                                text = InputStr;
                            }
                        }
                    }
                }
                result = text;
            }
            catch
            {
                result = DateTime.Parse(InputStr).ToShortDateString();
            }
            return result;
        }
        public int GetConfigPriceWhenChange()
        {
            int result = 0;
            try
            {
                SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
                try { if (con.State != ConnectionState.Open)  con.Open(); }
                catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }
                SqlCommand sqlCommand = con.CreateCommand();
                sqlCommand.CommandText = "SELECT ISNULL(PriceWhenChange,0) as PriceWhenChange FROM CONFIGINFO";
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    result = int.Parse(sqlDataReader["PriceWhenChange"].ToString());
                }
                sqlDataReader.Close();
                con.Close();
            }
            catch
            {
            }
            return result;
        }
        public void UpdateConfigPriceWhenChange(int tempValue)
        {
            SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
            try { if (con.State != ConnectionState.Open)  con.Open(); }
            catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }
            SqlCommand sqlCommand = con.CreateCommand();
            sqlCommand.CommandText = "UPDATE  [CONFIGINFO] SET [PriceWhenChange]=" + tempValue;
            sqlCommand.ExecuteNonQuery();
            con.Close();
        }
        public string DateFormatingToWin(string InputStr)
        {
            string result="";
            CONFIGDATEINFO u = GetObjDate();
            if (u != null)
            {
                string typeDate = u.TypeDate.ToString();
                if (typeDate == "1")
                {
                    if (InputStr.Substring(1, 1) == "/" && InputStr.Substring(3, 1) == "/")
                    {
                        result = string.Concat(new string[]
					{
						"0",
						InputStr.Substring(2, 1),
						"/0",
						InputStr.Substring(0, 1),
						"/",
						InputStr.Substring(4, 4)
					});
                    }
                    else
                    {
                        if (InputStr.Substring(2, 1) == "/" && InputStr.Substring(4, 1) == "/")
                        {
                            result = string.Concat(new string[]
						{
							"0",
							InputStr.Substring(3, 1),
							"/",
							InputStr.Substring(0, 2),
							"/",
							InputStr.Substring(5, 4)
						});
                        }
                        else
                        {
                            if (InputStr.Substring(1, 1) == "/" && InputStr.Substring(4, 1) == "/")
                            {
                                result = string.Concat(new string[]
							{
								InputStr.Substring(2, 2),
								"/0",
								InputStr.Substring(0, 1),
								"/",
								InputStr.Substring(5, 4)
							});
                            }
                            else
                            {
                                if (InputStr.Substring(2, 1) == "/" && InputStr.Substring(5, 1) == "/")
                                {
                                    result = string.Concat(new string[]
								{
									InputStr.Substring(3, 2),
									"/",
									InputStr.Substring(0, 2),
									"/",
									InputStr.Substring(6, 4)
								});
                                }
                                else
                                {
                                    result = InputStr.Substring(0, InputStr.IndexOf(":") - 3);
                                }
                            }
                        }
                    }
                }
                else
                {
                    result = InputStr.Substring(0, InputStr.IndexOf(":") - 3);
                }
            }
            return result;
        }
        public string CryptographyMD5(string source)
        {
            MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.UTF8.GetBytes(source);
            byte[] array = mD5CryptoServiceProvider.ComputeHash(bytes);
            string text = "";
            byte[] array2 = array;
            for (int i = 0; i < array2.Length; i++)
            {
                byte b = array2[i];
                text += int.Parse(b.ToString(), NumberStyles.HexNumber).ToString();
            }
            return text;
        }

        public string getValue(string tableName, string fieldName, string conditionName, string valueCompare)
        {
            string result = "";
            SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
            try { if (con.State != ConnectionState.Open)  con.Open(); }
            catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }

            SqlCommand sqlCommand = con.CreateCommand();
            string commandText = string.Concat(new string[]
			{
				"SELECT [",
				fieldName,
				"] FROM [",
				tableName,
				"] WHERE [",
				conditionName,
				"] = '",
				valueCompare,
				"'"
			});
            try
            {
                sqlCommand.CommandText = commandText;
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    result = sqlDataReader[fieldName].ToString();
                }
                sqlDataReader.Close();
                sqlDataReader.Dispose();
            }
            catch
            {
            }
            con.Close();
            
            return result;
        }
        public string getValueNumber(string objSQL, string fieldName)
        {
            string result = "0";
            SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
            try { if (con.State != ConnectionState.Open)  con.Open(); }
            catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }

            SqlCommand sqlCommand = con.CreateCommand();
            try
            {
                sqlCommand.CommandText = objSQL;
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    result = sqlDataReader["" + fieldName + ""].ToString();
                }
                sqlDataReader.Close();
                sqlDataReader.Dispose();
            }
            catch
            {
            }
            con.Close();
            
            return result;
        }
        public void LoadComboBox(string SQL, ComboBox objCbb, string _ValueNumber, string _ValueText)
        {
            objCbb.BindingContext = new BindingContext();
            SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
            try { if (con.State != ConnectionState.Open)  con.Open(); }
            catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(SQL,con);
            DataSet dataSet = new DataSet();
            try
            {
                sqlDataAdapter.Fill(dataSet);
                objCbb.DataSource = dataSet.Tables[0].DefaultView;
                objCbb.ValueMember = _ValueNumber;
                objCbb.DisplayMember = _ValueText;
                objCbb.SelectedIndex = 0;
            }
            catch
            {
            }
            finally
            {
                dataSet.Dispose();
                con.Close();
                
            }
        }
        public void SelectValueCombobox(ComboBox objCbb, string objValue)
        {
            for (int i = 0; i < objCbb.Items.Count; i++)
            {
                ItemCombobox itemCombobox = (ItemCombobox)objCbb.Items[i];
                if (itemCombobox.Value == objValue)
                {
                    objCbb.SelectedIndex = i;
                    break;
                }
            }
        }
        public void LoadAutoCompleteTextBox(string SQL, TextBox txt, string _ValueText)
        {
            SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
            try { if (con.State != ConnectionState.Open)  con.Open(); }
            catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }

            AutoCompleteStringCollection autoCompleteStringCollection = new AutoCompleteStringCollection();
            SqlCommand sqlCommand = con.CreateCommand();
            sqlCommand.CommandText = SQL;
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            if (sqlDataReader.HasRows)
            {
                while (sqlDataReader.Read())
                {
                    autoCompleteStringCollection.Add(sqlDataReader[_ValueText].ToString());
                }
            }
            sqlDataReader.Close();
            sqlDataReader.Dispose();
            con.Close();
            
            txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txt.AutoCompleteCustomSource = autoCompleteStringCollection;
        }
        public string VND(float NumCurrency)
        {
            string text = "";
            string text2 = "";
            string[] array = new string[10];
            int num = 0;
            int num2 = 0;
            int num3 = 0;
            int num4 = 0;
            int num5 = 0;
            int num6 = 0;
            int num7 = 0;
            string text3 = "đồng";
            string text4 = "xu";
            if (NumCurrency != 0f)
            {
                if ((double)NumCurrency <= 922337203685477.0)
                {
                    array[1] = "một";
                    array[2] = "hai";
                    array[3] = "ba";
                    array[4] = "bốn";
                    array[5] = "năm";
                    array[6] = "sáu";
                    array[7] = "bảy";
                    array[8] = "tám";
                    array[9] = "chín";
                    num = (int)((NumCurrency - (float)((int)NumCurrency)) * 100f);
                    string text5 = NumCurrency.ToString().Trim();
                    string str = new string(' ', 15 - text5.Length);
                    text5 = str + text5;
                    try
                    {
                        num3 = int.Parse(text5.Substring(0, 3));
                    }
                    catch
                    {
                        num3 = 0;
                    }
                    try
                    {
                        num4 = int.Parse(text5.Substring(3, 3));
                    }
                    catch
                    {
                        num4 = 0;
                    }
                    try
                    {
                        num5 = int.Parse(text5.Substring(6, 3));
                    }
                    catch
                    {
                        num5 = 0;
                    }
                    try
                    {
                        num6 = int.Parse(text5.Substring(9, 3));
                    }
                    catch
                    {
                        num6 = 0;
                    }
                    try
                    {
                        num7 = int.Parse(text5.Substring(12, 3));
                    }
                    catch
                    {
                        num7 = 0;
                    }
                    int i;
                    if (num3 == 0 && num4 == 0 && num5 == 0 && num6 == 0 && num7 == 0)
                    {
                        text2 = "Không " + text3;
                        i = 5;
                    }
                    else
                    {
                        text2 = "";
                        i = 0;
                    }
                    while (i <= 5)
                    {
                        switch (i)
                        {
                            case 0:
                                num2 = num3;
                                text = "nghìn tỷ ";
                                break;
                            case 1:
                                num2 = num4;
                                text = "tỷ ";
                                break;
                            case 2:
                                num2 = num5;
                                text = "triệu ";
                                break;
                            case 3:
                                num2 = num6;
                                text = "nghìn";
                                break;
                            case 4:
                                num2 = num7;
                                text = text3;
                                break;
                            case 5:
                                num2 = num;
                                text = text4;
                                break;
                        }
                        if (num2 != 0)
                        {
                            int num8 = num2 / 100;
                            int num9 = (num2 - num8 * 100) / 10;
                            int num10 = num2 - num8 * 100 - num9 * 10;
                            text2 = text2.Trim() + ((text2.Length == 0) ? "" : " ") + ((num8 != 0) ? (array[num8].Trim() + " trăm ") : " không trăm ");
                            if (num9 == 0 & num8 != 0 & num10 != 0)
                            {
                                text2 += "lẻ ";
                            }
                            else
                            {
                                if (num9 != 0)
                                {
                                    text2 += ((num9 != 0 & num9 != 1) ? (array[num9].Trim() + " mười ") : "mười ");
                                }
                            }
                            if (num9 != 0 & num10 == 5)
                            {
                                text2 = text2 + "lăm " + text + " ";
                            }
                            else
                            {
                                if (num9 > 1 & num10 == 1)
                                {
                                    text2 = text2 + "mốt " + text + " ";
                                }
                                else
                                {
                                    text2 += ((num10 != 0) ? (array[num10].Trim() + " " + text + " ") : (text + " "));
                                }
                            }
                        }
                        else
                        {
                            text2 += ((i == 4) ? (text3 + " ") : "");
                        }
                        i++;
                    }
                }
            }
            return text2;
        }
        public string Convert2SQLDateTime24(string sDate)
        {
            string result;
            try
            {
                DateTime.Parse("12/10/2014");
                result = "CONVERT(DATETIME, '" + sDate + " 12:00:00 AM', 102)";
            }
            catch
            {
                result = "CONVERT(DATETIME, '" + sDate + " 12:00:00 AM', 103)";
            }
            return result;
        }
        public string changeNumberToWord(float NumCurrency)
        {
            string text = "";
            string text2 = "";
            string[] array = new string[10];
            int num = 0;
            int num2 = 0;
            int num3 = 0;
            int num4 = 0;
            int num5 = 0;
            int num6 = 0;
            int num7 = 0;
            int num8 = 0;
            int num9 = 0;
            int num10 = 0;
            string text3 = "Đồng";
            string text4 = "xu";
            if (NumCurrency != 0f)
            {
                if ((double)NumCurrency <= 922337203685477.0)
                {
                    array[1] = "Một";
                    array[2] = "Hai";
                    array[3] = "Ba";
                    array[4] = "Bốn";
                    array[5] = "Năm";
                    array[6] = "Sáu";
                    array[7] = "Bảy";
                    array[8] = "Tám";
                    array[9] = "Chín";
                    num = (int)((NumCurrency - (float)((int)NumCurrency)) * 100f);
                    string text5 = NumCurrency.ToString().Trim();
                    string str = new string(' ', 15 - text5.Length);
                    text5 = str + text5;
                    try
                    {
                        num3 = int.Parse(text5.Substring(0, 3));
                    }
                    catch
                    {
                        num3 = 0;
                    }
                    try
                    {
                        num4 = int.Parse(text5.Substring(3, 3));
                    }
                    catch
                    {
                        num4 = 0;
                    }
                    try
                    {
                        num5 = int.Parse(text5.Substring(6, 3));
                    }
                    catch
                    {
                        num5 = 0;
                    }
                    try
                    {
                        num6 = int.Parse(text5.Substring(9, 3));
                    }
                    catch
                    {
                        num6 = 0;
                    }
                    try
                    {
                        num7 = int.Parse(text5.Substring(12, 3));
                    }
                    catch
                    {
                        num7 = 0;
                    }
                    int i;
                    if (num3 == 0 && num4 == 0 && num5 == 0 && num6 == 0 && num7 == 0)
                    {
                        text2 = "Không " + text3;
                        i = 5;
                    }
                    else
                    {
                        text2 = "";
                        i = 0;
                    }
                    while (i <= 5)
                    {
                        switch (i)
                        {
                            case 0:
                                num2 = num3;
                                text = "Nghìn tỷ";
                                break;
                            case 1:
                                num2 = num4;
                                text = "Tỷ ";
                                break;
                            case 2:
                                num2 = num5;
                                text = "Triệu ";
                                break;
                            case 3:
                                num2 = num6;
                                text = "Nghìn";
                                break;
                            case 4:
                                num2 = num7;
                                text = text3;
                                break;
                            case 5:
                                num2 = num;
                                text = text4;
                                break;
                        }
                        if (num2 != 0)
                        {
                            num8 = num2 / 100;
                            num9 = (num2 - num8 * 100) / 10;
                            num10 = num2 - num8 * 100 - num9 * 10;
                            try
                            {
                                text2 = text2.Trim() + ((text2.Length == 0) ? "" : " ") + ((num8 != 0) ? (array[num8].Trim() + " Trăm ") : "");
                            }
                            catch
                            {
                            }
                            if (num9 == 0 & num8 != 0 & num10 != 0)
                            {
                                text2 += " Lẻ ";
                            }
                            else
                            {
                                if (num9 != 0)
                                {
                                    try
                                    {
                                        text2 += ((num9 != 0 & num9 != 1) ? (array[num9].Trim() + " Mười ") : "Mười ");
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                            if (num9 != 0 & num10 == 5)
                            {
                                text2 = text2 + "Lăm " + text + " ";
                            }
                            else
                            {
                                if (num9 > 1 & num10 == 1)
                                {
                                    text2 = text2 + "Mốt " + text + " ";
                                }
                                else
                                {
                                    try
                                    {
                                        text2 += ((num10 != 0) ? (array[num10].Trim() + " " + text + " ") : (text + " "));
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                        }
                        else
                        {
                            text2 += ((i == 4) ? (text3 + " ") : "");
                        }
                        i++;
                    }
                }
            }
            return text2;
        }
        private string ChangeNumToWord(int NumVar)
        {
            string text = "";
            int num = int.Parse((NumVar / 100).ToString());
            int num2 = int.Parse((NumVar % 100 / 10).ToString());
            int num3 = NumVar % 10;
            if (num == 0 && num2 == 0 && num3 == 0)
            {
                text = "";
            }
            if (num != 0)
            {
                text = text + this.numberArr[num].ToString() + " Trăm ";
                if (num2 == 0 && num3 != 0)
                {
                    text += "Lẻ ";
                }
            }
            if (num2 != 0 && num2 != 1)
            {
                text = text + this.numberArr[num2].ToString() + " Mươi ";
                if (num2 == 0 && num3 != 0)
                {
                    text += " Lẻ ";
                }
            }
            if (num2 == 1)
            {
                text += " Mười ";
            }
            int num4 = num3;
            if (num4 != 1)
            {
                if (num4 != 5)
                {
                    text += this.numberArr[num3].ToString();
                }
                else
                {
                    if (num2 == 0)
                    {
                        text += this.numberArr[num3].ToString();
                    }
                    else
                    {
                        text += " Lăm ";
                    }
                }
            }
            else
            {
                if (num2 != 0 && num2 != 1)
                {
                    text += " Mốt ";
                }
                else
                {
                    text += this.numberArr[num3].ToString();
                }
            }
            return text;
        }
        public string changeNumberToWord1(long NumberChange)
        {
            string text = "";
            int[] array = new int[4];
            this.numberArr[0] = "";
            this.numberArr[1] = "Một ";
            this.numberArr[2] = "Hai ";
            this.numberArr[3] = "Ba ";
            this.numberArr[4] = "Bốn ";
            this.numberArr[5] = "Năm ";
            this.numberArr[6] = "Sáu ";
            this.numberArr[7] = "Bảy ";
            this.numberArr[8] = "Tám ";
            this.numberArr[9] = "Chín ";
            this.moneyArr[0] = "Đồng ";
            this.moneyArr[1] = "Ngàn ";
            this.moneyArr[2] = "Triệu ";
            this.moneyArr[3] = "Tỷ ";
            if (NumberChange < 0L)
            {
                text = "Trừ ";
            }
            if (Math.Abs(NumberChange) > 999999999999L)
            {
                NumberChange = 0L;
            }
            long num = Math.Abs(NumberChange);
            array[3] = int.Parse((num / 1000000000L).ToString());
            num -= long.Parse(array[3].ToString() + "000000000");
            array[2] = int.Parse((num / 1000000L).ToString());
            array[1] = int.Parse((num % 1000000L / 1000L).ToString());
            array[0] = int.Parse((num % 1000L).ToString());
            int num2;
            if (array[3] > 0)
            {
                num2 = 3;
            }
            else
            {
                if (array[2] > 0)
                {
                    num2 = 2;
                }
                else
                {
                    if (array[1] > 0)
                    {
                        num2 = 1;
                    }
                    else
                    {
                        num2 = 0;
                    }
                }
            }
            for (int i = num2; i >= 0; i--)
            {
                try
                {
                    text += this.ChangeNumToWord(array[i]);
                }
                catch
                {
                }
                text = text + this.moneyArr[i].ToString() + " ";
            }
            text = text.ToLower().Trim();
            text = text.Substring(0, 1).ToUpper() + text.Remove(0, 1);
            return text.Trim().Replace("  ", " ").Replace("triệu ngàn đồng", "triệu đồng").Replace("tỷ triệu ngàn đồng", "tỷ đồng");
        }
        public string Convert2SQLDateTime(string sDate)
        {
            string result;
            try
            {
                DateTime.Parse("12/15/2009");
                result = "CONVERT(DATETIME, '" + sDate + " 00:00:00', 102)";
            }
            catch
            {
                result = "CONVERT(DATETIME, '" + sDate + " 00:00:00', 103)";
            }
            return result;
        }
        public string Convert2SQLDateTimesTART(string sDate)
        {
            string result;
            try
            {
                DateTime.Parse("12/15/2009");
                result = "CONVERT(DATETIME, '" + sDate + " 06:00:00 AM', 102)";
            }
            catch
            {
                result = "CONVERT(DATETIME, '" + sDate + " 06:00:00 AM', 103)";
            }
            return result;
        }
        public string Convert2SQLDateTimesTOP(string sDate)
        {
            string result;
            try
            {
                DateTime.Parse("12/15/2009");
                result = "CONVERT(DATETIME, '" + sDate + " 00:00:00 AM', 102)";
            }
            catch
            {
                result = "CONVERT(DATETIME, '" + sDate + " 00:00:00 AM', 103)";
            }
            return result;
        }
        public string Convert2SQLDateTime1(string sDate)
        {
            string result;
            try
            {
                DateTime.Parse("12/15/2009");
                result = "CONVERT(DATETIME, '" + sDate + " 12:00:00 AM', 102)";
            }
            catch
            {
                result = "CONVERT(DATETIME, '" + sDate + " 12:00:00 AM', 103)";
            }
            return result;
        }

        public void DefineArray(string[] ArrayName, int Count)
        {
            try
            {
                for (int i = 0; i < Count; i++)
                {
                    ArrayName[i] = "NULL";
                }
            }
            catch
            {
            }
        }
        public void DefineArrayNumber(string[] ArrayName, int Count)
        {
            try
            {
                for (int i = 0; i < Count; i++)
                {
                    ArrayName[i] = "0";
                }
            }
            catch
            {
            }
        }

        public void setShowItemByGroup(int HienTheoNhom)
        {
            try
            {
                SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
                try { if (con.State != ConnectionState.Open)  con.Open(); }
                catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }
                SqlCommand sqlCommand = con.CreateCommand();
                sqlCommand.CommandText = "UPDATE CONFIGINFO SET ShowItemByGroup = @ShowItemByGroup";
                sqlCommand.Parameters.Add("ShowItemByGroup", SqlDbType.SmallInt).Value = HienTheoNhom;
                sqlCommand.ExecuteNonQuery();
                con.Close();
                
            }
            catch
            {
            }
        }
        public int getShowItemByGroup()
        {
            int result = 0;
            try
            {
                SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
                try { if (con.State != ConnectionState.Open)  con.Open(); }
                catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }
                SqlCommand sqlCommand = con.CreateCommand();
                sqlCommand.CommandText = "SELECT ISNULL(showItemByGroup,0) as showItemByGroup FROM CONFIGINFO";
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    result = int.Parse(sqlDataReader["showItemByGroup"].ToString());
                }
                sqlDataReader.Close();
                con.Close();
                
            }
            catch
            {
            }
            return result;
        }
        public int getBillScrollType()
        {
            int result = 0;
            try
            {
                SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
                try { if (con.State != ConnectionState.Open)  con.Open(); }
                catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }
                SqlCommand sqlCommand = con.CreateCommand();
                sqlCommand.CommandText = "SELECT ISNULL(BillScrollType,0) as BillScrollType FROM CONFIGINFO";
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    result = int.Parse(sqlDataReader["BillScrollType"].ToString());
                }
                sqlDataReader.Close();
                con.Close();
                
            }
            catch
            {
            }
            return result;
        }
        public void setBillScrollType(int LoaiGiayCuon)
        {
            try
            {
                SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
                try { if (con.State != ConnectionState.Open)  con.Open(); }
                catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }

                SqlCommand sqlCommand = con.CreateCommand();
                sqlCommand.CommandText = "UPDATE CONFIGINFO SET BillScrollType = @BillScrollType";
                sqlCommand.Parameters.Add("BillScrollType", SqlDbType.SmallInt).Value = LoaiGiayCuon;
                sqlCommand.ExecuteNonQuery();
                con.Close();
                
            }
            catch
            {
            }
        }

        public void getOtherMoney(out bool MoneyByPercent,out string OtherMoney)
        {
            MoneyByPercent = false;
            OtherMoney = "";
            try
            {
                SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
                try { if (con.State != ConnectionState.Open)  con.Open(); }
                catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }
                SqlCommand sqlCommand = con.CreateCommand();
                sqlCommand.CommandText = "SELECT OtherMoney, calByPercent FROM CONFIGINFO";
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    OtherMoney = sqlDataReader["OtherMoney"].ToString();
                    if (sqlDataReader["calByPercent"].ToString() == "1")
                    {
                        MoneyByPercent = true;
                    }
                    else
                    {
                        MoneyByPercent = false;
                    }
                }
                sqlDataReader.Close();
                con.Close();
                //password_server
            }
            catch
            {
            }
        }
        public void updateOtherMoney(string OtherMoney, bool MoneyByPercent)
        {
            if (OtherMoney.Trim() == "")
            {
                MessageBox.Show("Nhập tỉ lệ số tiền phụ trên 1 hóa đơn !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            else
            {
                try
                {
                    double num = double.Parse(OtherMoney);
                    if (num < 0.0)
                    {
                        MessageBox.Show("Giá trị phụ thu phải lớn hơn 0 !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
                catch
                {
                    MessageBox.Show("Nhập tỉ lệ số tiền phụ trên 1 hóa đơn !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                SqlConnection con = (SqlConnection)ConnectionProvider.ServerConnection;
                try { if (con.State != ConnectionState.Open)  con.Open(); }
                catch { throw new Exception("Không thể kết nối đến cơ sở dữ liệu"); }

                int num2;
                if (MoneyByPercent)
                {
                    num2 = 1;
                }
                else
                {
                    num2 = 0;
                }
                SqlCommand sqlCommand = con.CreateCommand();
                sqlCommand.CommandText = "UPDATE CONFIGINFO SET OtherMoney = @OtherMoney, calByPercent = @calByPercent";
                sqlCommand.Parameters.Add("OtherMoney", SqlDbType.Float).Value = double.Parse(OtherMoney);
                sqlCommand.Parameters.Add("calByPercent", SqlDbType.SmallInt).Value = num2;
                sqlCommand.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}
